type datas =  {
    start:number,
    id:string,
    actionStartTime?:string
    startHour:number,
    release:number,
    endDate:string,
    index:number,
}

type week = {
    id?:string;
    fullDate?:string,
    hours?:string,
    startDate?:string,
    start:number,
    startHour?:number,
    end:number,
    release:number,
    endDate:string,
    index:number,
}

interface sourch {
    id?:string;
    hours?:string;
    startDate?:string;
    start:number;
    startHour:number;
    release:number,
    endDate:string,
    index:number,
}

interface eData  {
    startDate?:string;
    endDate?:string,
    hours:string
    start:number;
    end:number;
    index:number;

    
}

interface day{
    endDate?:string;
    end:number;
    release?:number;
    start:number;
    startDate?:string;
    id:string,
    index?:number,
}


export const overWei = (month:[[]],dayData:datas[],overData:number,aindex:number,dayEvent:object[],e:eData)=>{
    // 循环将数据进行赋值
    const numberMonth:number = overData;
    const weekData:week[] = month[numberMonth];
    const endLoadingData = e.endDate
    // debugger
    if(aindex===6){
        // 不存在进入下一行的情况,下一行会继续操作

    }else{
        let allSourchData = [];
        /**
         * 判断是否在本周,不在本周就从week0开始算起
         * 判断下一天是否有上午,如果有则断开,断掉尾巴,如果没有则继续找,找到为止(本周最后一天)
         * */ 
            // 判断开始时间是否在本周,如果是则从这一天开始 如果不是则跑到下一天
            let myINdex = weekData.findIndex((item,index)=>{
                return item.fullDate === e.startDate
            })
            let myEndINdex = weekData.findIndex((item,index)=>{
                return item.fullDate === e.endDate
            })
            // 如果是-1则从第一天算起,如果为0以上则从0以上算起
            // 循环找出数据
            myINdex = myINdex===-1?0:myINdex;
            let numberAdd = myINdex
            myEndINdex = myEndINdex===-1?6:myEndINdex
            for(let a =myINdex;a<myEndINdex+1;a++ ){
               const linNumber =  getAnyTimeEventNumber(weekData[a].fullDate, dayEvent);
             
               const isMorning = linNumber.findIndex((item,index)=>{
                
                return item.startHour < 43200
               })
               if(endLoadingData === weekData[a].fullDate&&isMorning==-1){
                return {
                    over:false
                }
               }
               if(isMorning!=-1){
                // debugger
               }
               if(isMorning!=-1){
                break;
               }else{
                if(numberAdd===6){
                    break
                }
                numberAdd = numberAdd + 1
                continue
               }
             
            }
            // 截尾
            if(numberAdd===6){
                // 尾巴断完了
                return {
                    over:true,
                    overWei:true

                } 
            }else{
                let endWei = weekData[numberAdd-1];
                // debugger
                let endDate = endWei.fullDate;
                let newTime = new Date(endDate + " " + e.hours?.split("-")[1]) as any;
                let overData = JSON.stringify(e);
                let overFireData = JSON.parse(overData)
                let day:day = overFireData
                day.endDate = endDate
                // 把尾巴断掉,并生成新的尾巴
                const endStart = new Date(endDate + "23:59:59").getTime();
                
                // let startDate = weekData[myINdex+1];//只是他的日期
                // let day = sourchData[j];
                // let newTime = new Date(startDate.fullDate + " " + sourchData[j].hours?.split("-")[0]) as any;
                // day.startDate = startDate.fullDate;
                // const timeStart = new Date(startDate.fullDate + " 00:00:00").getTime();
                // if (isNaN(newTime) || newTime < endStart) {
                //   return false;
                // }
                
                let actionStartTime = format(new Date(newTime), "yyyy-MM-dd HH:mm:ss");
                day.end = new Date(actionStartTime).getTime();
                // const startHourSplit = startHour.split(":");
                day.release = day.end - day.start;
                // push新的数据
                // 这里为断臂的数据
                let pushStatus = false;
                let overData1 = JSON.stringify(e);
                let overFireData1 = JSON.parse(overData1)
                let baseDay:day = overFireData1;
                if(numberAdd===6){
                    // 这里就不存在断尾了
                    return {
                        over:true,
                        overWei:true
                    }
                }else{
                    pushStatus = true
                    let stateWei1 = weekData[numberAdd];
                    let startDate1 = stateWei1.fullDate;
                    let newTime1 = new Date(startDate1 + " " + e.hours?.split("-")[0]) as any;
                    baseDay.startDate = startDate1;
                    let actionStartTime1 = format(new Date(newTime1), "yyyy-MM-dd HH:mm:ss");
                    baseDay.start = new Date(actionStartTime1).getTime();
                    // const startHourSplit = startHour.split(":");
                    baseDay.release = baseDay.end - baseDay.start;
                    baseDay.id=baseDay.id+baseDay.id;
                    if(baseDay.index!=undefined){
                        delete baseDay.index
                    }
                }
            
                // 这里继续操作
                if(pushStatus){
                    return {
                        day:day,
                        pushStatus,
                        baseDay,
                        over:true
                        
                    }
                }else{
                    return {
                        day:day,
                        pushStatus,
                        over:true
                    } 
                }
            }
           



    }

}

// 格式化事件
export const format = (date:any, fmt:any) => {         
    var o:any = {         
        "M+" : date.getMonth() + 1, //月份         
        "d+" : date.getDate(), //日         
        "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时         
        "H+" : date.getHours(), //小时         
        "m+" : date.getMinutes(), //分         
        "s+" : date.getSeconds(), //秒         
        "q+" : Math.floor((date.getMonth()+3)/3), //季度         
        "S" : date.getMilliseconds() //毫秒         
    };         
    var week:any = {         
        "0" : "/u65e5",         
        "1" : "/u4e00",         
        "2" : "/u4e8c",         
        "3" : "/u4e09",         
        "4" : "/u56db",         
        "5" : "/u4e94",         
        "6" : "/u516d"        
    };         
    if(/(y+)/.test(fmt)){         
        fmt = fmt.replace(RegExp.$1, (date.getFullYear()+"").substring(4 - RegExp.$1.length));         
    }         
    if(/(E+)/.test(fmt)){         
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getDay()+""]);         
    }         
    for(var k in o){         
        if(new RegExp("("+ k +")").test(fmt)){         
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substring((""+ o[k]).length)));         
        }         
    }         
    return fmt;         
}
export const getAnyTimeEventNumber = (date:any, events:any, str = 'day', AddTime = 0) => {
    let start:any = null
    let end:any = null
    if (!date) {
        start = new Date(new Date().toLocaleDateString()).getTime()
        end = start + 24 * 60 * 60 * 1000 - 1
    } else {
        if (typeof date !== 'object') {
            date = date.replace(/-/g, '/')
        }
        if (str == 'day') {
        
            start = new Date(new Date(date).toLocaleDateString()).getTime()
            end = start + 24 * 60 * 60 * 1000 - 1
        } else if (str == 'time') {
          
            // 根据1小时获取
            start = new Date(date).getTime()
            if (AddTime) {
                end = start + AddTime
            } else {
                // 默认1小时
                end = start + 60 * 60 * 1000 - 1
            }
        }
    }
    // 检索的时候加入weileistart
    let deArr = [] // 结果数据
    for (let i = 0; i < events.length; i++) {
        const event_start = events[i].start;
        const event_end = events[i].end
        if (
            (event_start >= start && event_end <= end) ||
            (event_start >= start && event_end >= end && event_start <= end) ||
            (event_start <= start && event_end >= end) ||
            (event_start <= start && event_end <= end && event_end >= start)
        ) {
            deArr.push(events[i])
        }
    }
    return deArr
}

interface warning {
    priority?:number|string,
    startHour:number|string,
    warning?:number|string,
    index:number,
    id:string,
    isType:number,
    isWarrEdit:number,
}

// interface day_event{
   
// }
// 将重要事项排列到前排
export const warningEvent = (onther:warning[],halfNum:number) =>{
    let onthers:warning[] = onther;
    // 把上午的循环出来
    let after:warning[] = [];
    let mor:warning[] = [];
    for(let i = 0 ;i<halfNum;i++){
        // 将两个都添加进去
        mor.push(onther[i]);
        after.push(onther[i+halfNum]);
    };
    let ontherdata:warning[] = []
    if(onther.length>halfNum*2){
        ontherdata = onthers.splice(halfNum*2,onther.length-1);
    }else{
        ontherdata = []
    }
    let handler:warning[] = []
    // 先筛选出重要的事项
    for(let i:number = 0 ; i<ontherdata.length;i++){
        // if(ontherdata[i].id=  ,,,,,,,,,,,,,,,,,,='1640273194048249857'){
        //     debugger
        // }
        if(Number(ontherdata[i].priority)===1){
            // 说明很重要
            // 判断数组内是否全天的 在判断是否是多天的（index） 最后判断其他元素是否有重要事项
            // 标记一下位置
            let index:Array<string|number> = [];
            for(let i = 0 ; i <halfNum;i++){
                index.push('0');
            }
            
            if(ontherdata[i].startHour >= 43200){
                for(let j =0 ; j <after.length;j++){
                    if(Number(after[j].warning)===1||after[j].index!=undefined||after[j].isWarrEdit==1){
                        // 把他的位置push进去
                        if(after[j].index!=undefined){
                            index[after[j].index] = after[j].id;
                        }else if(Number(after[j].warning)===1){
                            const num = index.findIndex((item,index)=>{
                                return item == '0'
                            });
                            index[num] = after[j].id;
                        }
                    }
                }
                const overNum = index.findIndex((item,index)=>{
                    return item == '0'
                });
                if(overNum === -1){
                    // 没戏，人家早满了
                }else{
                    // 有戏 将after位置换一下,下次再用呗 这里本来用的findIndex 现在用for;
                    let indexNumber:number = -1
                    for(let w = index.length-1 ; w >=0 ;w++){
                        if(index[w]==='0'){
                            indexNumber = w;
                            break;
                        }
                    }
                    // const num = index.findIndex((item,index)=>{
                    //     return item == '0'
                    // });
                    // 将两个位置对调一下
                    if(indexNumber!=-1){
                        // 到这一步不会出现问题的，我的程序灰常健壮
                        let afterData  = after[indexNumber];
                        // 然后原始数据去坑里吃土;
                        ontherdata[i].isType===1;
                        ontherdata[i].isWarrEdit===1;
                        after[indexNumber] = ontherdata[i];
                        handler.push(afterData);
                    }
                    // const Ndata = after[]
                    // after.
                }
            }else{
                for(let j =0 ; j <mor.length;j++){
                    if(Number(mor[j].warning)===1||mor[j].index!=undefined){
                        // 把他的位置push进去
                        if(mor[j].index!=undefined){
                            index[mor[j].index] = mor[j].id;
                        }else if(Number(mor[j].warning)===1){
                            const num = index.findIndex((item,index)=>{
                                return item == '0'
                            });
                            index[num] = mor[j].id;
                        }
                    }
                }
                const overNum = index.findIndex((item,index)=>{
                    return item == '0'
                });
                if(overNum===-1){
                    // 没戏，人家早满了
                }else{
                    let indexNumber:number = -1
                    for(let w = index.length-1 ; w >=0 ;w++){
                        if(index[w]==='0'){
                            indexNumber = w;
                            break;
                        }
                    }
                    // 有戏 将after位置换一下,下次再用呗
                    // const num = index.findIndex((item,index)=>{
                    //     return item == '0'
                    // });
                    // 将两个位置对调一下
                    if(indexNumber!=-1){
                        // 到这一步不会出现问题的，我的程序灰常健壮
                        let afterData  = mor[indexNumber];
                        // 然后原始数据去坑里吃土;
                        ontherdata[i].isType===1;
                        ontherdata[i].isWarrEdit===1;
                        mor[indexNumber] = ontherdata[i];
                        handler.push(afterData);
                    }
                    // const Ndata = after[]
                    // after.
                }
            }
            
        }
    }
    let conData =mor.concat(after).concat(ontherdata).concat(handler);
    const items = new Set(conData);

   
    let arrSelect = Array.from(items);

    return arrSelect
}